// import { RouteRecordRaw } from "vue-router";
import { HOME_URL } from "@/config/config";
import router from "@/router/index";
import { flatRouter, generateStaticRouterMenu } from "../../utils/menuUtil";

import { useAuthMenu } from "../../store/authMenu";

/**
 * staticRouter(静态路由)
 */
export const staticRouter = [
  {
    path: "/",
    redirect: HOME_URL,
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/Login.vue"),
    meta: {
      title: "登录",
    },
  },

  {
    path: "/:catchAll(.*)",
    component: () => import("@/views/error/404.vue"),
    meta: {
      title: "404",
    },
  },
  {
    path: "/layout",
    name: "layout",
    component: () => import("@/Layout/index.vue"),
    redirect: HOME_URL,
    children: [
      {
        path: "/home",
        name: "Home",
        meta: { title: "首页", icon: "HomeOutlined" },
        component: () => import("@/views/home/Home.vue"),
      },

      {
        path: "/components",
        name: "components",
        meta: { title: "组件", icon: "AppstoreAddOutlined" },
        children: [
          {
            path: "/common",
            name: "commonComponents",
            meta: { title: "通用组件" },
            component: () => import("@/views/componentView/Common.vue"),
          },
        ],
      },
      {
        path: "/profile",
        name: "Profile",
        meta: { title: "个人主页", icon: "UserOutlined" },
        component: () => import("@/views/profile/Profile.vue"),
      },
      {
        path: "/codeAssistant",
        name: "codeAssistant",
        meta: { title: "代码助手", icon: "RocketOutlined" },
        component: () => import("@/views/agent/CodeAssistant.vue"),
      },
      {
        path: "/system",
        name: "system",
        meta: { title: "系统管理", icon: "DesktopOutlined" },
        redirect: "/user",
        children: [
          {
            path: "/user",
            name: "user",
            meta: { title: "用户管理", isFull: false },
            component: () => import("@/views/system/User.vue"),
          },
          {
            path: "/role",
            name: "role",
            meta: { title: "角色管理" },
            component: () => import("@/views/system/Role.vue"),
          },
          {
            path: "/menu",
            name: "menu",
            meta: { title: "菜单管理", isFull: false },
            component: () => import("@/views/system/User.vue"),
          },
        ],
      },
    ],
  },
];

export const initStaticRouterAndMenu = () => {
  const authMenu = useAuthMenu();
  if (authMenu.staticMenuList) {
    return;
  }
  //将layout下的路由扁平化
  let routerList = staticRouter
    .filter((item) => {
      return item.name === "layout" && item.children.length > 0;
    })
    .map((item) => item.children)
    .flatMap((item) => item);
  //处理meta
  const flatMenuList = flatRouter(routerList);
  flatMenuList.forEach((item) => {
    if (item.meta?.isFull && router.hasRoute(item.name)) {
      router.removeRoute(item.name);
      router.addRoute(item);
    }
  });
  const menu = generateStaticRouterMenu();
  authMenu.staticMenuList = menu;
  authMenu.authMenuList = menu;
};
